﻿using Microsoft.WowAddonStudio.Lua.Parser;

namespace Microsoft.WowAddonStudio.Lua.Support
{
    public static class LexLocationExtensions
    {
        /// <summary>
        /// Determines whether a LexLocation is after a caret position.
        /// </summary>
        /// <param name="location">The location to test against.</param>
        /// <param name="line">The line number of the caret.</param>
        /// <param name="column">The column number of the caret.</param>
        /// <returns>True, if the caret is before the </returns>
        public static bool After(this LexLocation location, int line, int column)
        {
            if (location == null)
                return false;

            if (location.Line > line)
                return true;

            if (location.Line == line && location.Column > column)
                return true;

            return false;
        }

        /// <summary>
        /// Determines whether a LexLocation is before a caret position.
        /// </summary>
        /// <param name="location">The location to test against.</param>
        /// <param name="line">The line number of the caret.</param>
        /// <param name="column">The column number of the caret.</param>
        /// <returns>True, if the caret is before the </returns>
        public static bool Before(this LexLocation location, int line, int column)
        {
            if (location == null)
                return false;

            if (location.EndLine < line)
                return true;

            if (location.EndLine == line && location.EndColumn < column)
                return true;

            return false;
        }

        /// <summary>
        /// Determines whether a LexLocation contains a caret position.
        /// </summary>
        /// <param name="location">The location to test against.</param>
        /// <param name="line">The line number of the caret.</param>
        /// <param name="column">The column number of the caret.</param>
        /// <returns>True, if the caret is contained in the LexLocation; false otherwise.</returns>
        public static bool Contains(this LexLocation location, int line, int column)
        {
            if (location == null)
                return false;

            if (location.Line > line || location.EndLine < line)
                return false;

            if (location.Line == line && location.Column > column)
                return false;

            if (location.EndLine == line && location.EndColumn < column)
                return false;

            return true;
        }
    }
}
